package impl

import (
	"entry_task/backend/src/utils"
	"entry_task/entry_core/src/dao"
	"entry_task/entry_core/src/db"
	"entry_task/entry_core/src/model"
	"fmt"
	"time"
)

func init() {
	daoImpl := new(UserInfoDaoImpl)
	fmt.Println("i am daoimpl inject")
	dao.InjectAppManageDAO(daoImpl)
}

type UserInfoDaoImpl struct {
}

func (impl UserInfoDaoImpl) QueryById(id int64) *model.UserInfoTab {
	engine := db.DbSourceManager().GetDbSource("mysql")

	userInfo := new(model.UserInfoTab)
	has, err := engine.Id(id).Get(userInfo)
	utils.CheckErr(err)
	if has {
		return userInfo
	}
	return nil
}

func (impl UserInfoDaoImpl) QueryByUsername(username string) *model.UserInfoTab {
	engine := db.DbSourceManager().GetDbSource("mysql")
	userInfo := new(model.UserInfoTab)
	has, err := engine.Where("username=?", username).Get(userInfo)
	utils.CheckErr(err)
	if has {
		return userInfo
	}
	return nil
}

func (impl UserInfoDaoImpl) UpdateById(userInfo *model.UserInfoTab) int64 {
	engine := db.DbSourceManager().GetDbSource("mysql")
	userInfo.UpdateTime = time.Now()
	affected, err := engine.Id(userInfo.Id).Update(userInfo)
	utils.CheckErr(err)
	return affected
}

/**
测试用例，批量插入客户用,建议最多150条/次
*/
func (impl UserInfoDaoImpl) BatchInsert(users *[]model.UserInfoTab) int64 {
	engine := db.DbSourceManager().GetDbSource("mysql")
	affected, err := engine.Insert(&users)
	utils.CheckErr(err)
	return affected
}
